// pages/post/post.js
const DB=wx.cloud.database().collection("postList");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ischecked:1,
    title:'请帖',
    postList:[],
    model:false,
    shareName:'',
    shareId:'',
    tips:[]
  },

  /**取消分享模态框 */
  modelCancel(){
    this.setData({
      model:false
    })
  },

  /**
   * 选择菜单
   */
  checkMenu(e){
    wx.showLoading({
      title: '加载中',
    })
    const that=this;
    if(e.currentTarget.dataset.checked==1){
      that.reload();
    }else{
      that.receive();
    }
  },

  /**渲染收到请帖的数据 */
  receive(e){
    const openid=wx.getStorageSync('openid');
    const that=this;
    DB.where({
      receive:openid
    }).get({
      success(res){
        that.setData({
          postList:res.data,
          ischecked:2
        })
        wx.hideLoading()
      }
    })
  },

  /**
   * 单击条目事件
   */
  editList(res){
    wx.showLoading({
      title: '加载中',
    })
    const that=this;
    const {ischecked}=this.data;
    if(ischecked==1){
      that.createMenu(res);
    }else{
      that.receiveMenu(res);
    }
  },

  /**收到的请帖菜单 */
  receiveMenu(res){
    const that=this;
    const {id,event,user,receive}=res.currentTarget.dataset;
    wx.hideLoading()
    // 打开菜单
    wx.showActionSheet({
      alertText:'关于“'+user+'的'+event+'”',
      itemList: ['查看','删除'],
      itemColor:'#5EC2F6',
      success(e){
        if(e.tapIndex==0){
          wx.navigateTo({
            url: '/pages/showPost/showPost?id='+id
          })
        }else if(e.tapIndex==1){
          wx.showModal({
            title:'删除提示',
            content:'删除之后无法恢复，是否删除？',
            success (res) {
              if (res.confirm) {
                const openid=wx.getStorageSync('openid')
                const index=receive.indexOf(openid);
                receive.splice(index,1);
                DB.doc(id).update({
                  data:{
                    receive:receive
                  },
                  success(res){
                    wx.showToast({
                      title: '删除成功'
                    })
                    that.receive();
                  }
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      }
    })
  },

  /**创建的请帖菜单 */
  createMenu(res){
    console.log(res);
    const that=this;
    const {id,event,user}=res.currentTarget.dataset;
    wx.hideLoading()
    wx.showActionSheet({
      alertText:'关于“'+event+'”的请帖',
      itemList: ['查看','分享','编辑','删除'],
      itemColor:'#5EC2F6',
      success(e){
        if(e.tapIndex==0){
          wx.navigateTo({
            url: '/pages/showPost/showPost?id='+id
          })
        }else if(e.tapIndex==1){
          that.setData({
            model:true,
            shareName:user,
            shareId:id

          })
        }else if(e.tapIndex==2){
          wx.navigateTo({
            url: '/pages/addPost/addPost?id='+id
          })
        }else if(e.tapIndex==3){
          wx.showModal({
            title:'删除提示',
            content:'删除之后无法恢复，是否删除？',
            success (res) {
              if (res.confirm) {
                DB.doc(id).remove({
                  success(res){
                    wx.showToast({
                      title: '删除成功'
                    })
                    that.reload();
                  }
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      }
    })
  },

  /**重新渲染创建请帖的数据 */
  reload(){
    const openid=wx.getStorageSync('openid');
    const that=this;
    DB.where({
      _openid:openid
    }).get({
      success(res){
        that.setData({
          postList:res.data,
          ischecked:1
        })
        wx.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '加载中',
    })
    const {ischecked}=this.data;
    if(ischecked==1){
      this.reload();
    }else{
      this.receive();
    }
    const tips=wx.getStorageSync('tips');
    this.setData({
      tips:tips
    })
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const name=this.data.shareName;
    const id=this.data.shareId;
    this.setData({
      model:false
    })
    return {
      title: name+'分享给您的邀请函',
      path: "pages/showPost/showPost?id="+id+"&isShare="+true,
      imageUrl:'https://tworice.cn//upload/根目录/885d2378-37b6-4a6b-9fd8-0c76d254e034.png'
    }
  }
})